﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace ElingOA6_DSJCJ.DAL
{
	/// <summary>
	/// 数据访问类:PaperAnswerManager
	/// </summary>
	public partial class PaperAnswerManager
	{
		public PaperAnswerManager()
		{}
        #region 自增
        /// <summary>
        /// 分页获取文档列表
        /// <creator>fenghq</creator>
        /// <date>2013-02-28</date>
        /// </summary>
        /// <returns></returns>
        public List<Model.PaperAnswerManager> GetPaperListByPage(int pageSize, int pageIndex, string strWhere, string fOrderFiled, out int total)
        {
            if (fOrderFiled == "")
            {
                fOrderFiled = "FPaperAnswerManagerID";
            }
            SqlParameter[] parameters = {  
                    new SqlParameter("@tableName", SqlDbType.VarChar, 50),  
                    new SqlParameter("@fields", SqlDbType.VarChar, 2000),  
                    new SqlParameter("@orderField", SqlDbType.VarChar, 500),  
                    new SqlParameter("@sqlWhere", SqlDbType.VarChar,2000),  
                    new SqlParameter("@pageSize", SqlDbType.Int),  
                    new SqlParameter("@pageIndex", SqlDbType.Int),  
                    new SqlParameter("@TotalPage", SqlDbType.Int),
                    new SqlParameter("@TotalRecordCount", SqlDbType.Int) 
                    };

            parameters[0].Value = "t_PaperAnswerManager";
            parameters[1].Value = "*";
            parameters[2].Value = fOrderFiled;
            parameters[3].Value = strWhere;
            parameters[4].Value = pageSize;
            parameters[5].Value = pageIndex;
            parameters[6].Direction = ParameterDirection.Output;
            parameters[7].Direction = ParameterDirection.Output;
            DataSet ds = DbHelperSQL.RunProcedure("up_GetRecordByPage", parameters, "ds", 60 * 30);
            total = int.Parse(parameters[7].Value.ToString());
            return DataTableToList(ds.Tables[0]);

        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<ElingOA6_DSJCJ.Model.PaperAnswerManager> DataTableToList(DataTable dt)
        {
            List<ElingOA6_DSJCJ.Model.PaperAnswerManager> modelList = new List<ElingOA6_DSJCJ.Model.PaperAnswerManager>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                ElingOA6_DSJCJ.Model.PaperAnswerManager model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new ElingOA6_DSJCJ.Model.PaperAnswerManager();
                    if (dt.Rows[n]["FPaperAnswerManagerID"] != null && dt.Rows[n]["FPaperAnswerManagerID"].ToString() != "")
                    {
                        model.FPaperAnswerManagerID = decimal.Parse(dt.Rows[n]["FPaperAnswerManagerID"].ToString());
                    }
                    if (dt.Rows[n]["FPaperId"] != null && dt.Rows[n]["FPaperId"].ToString() != "")
                    {
                        model.FPaperId = decimal.Parse(dt.Rows[n]["FPaperId"].ToString());
                    }
                    if (dt.Rows[n]["FEntityNo"] != null && dt.Rows[n]["FEntityNo"].ToString() != "")
                    {
                        model.FEntityNo = dt.Rows[n]["FEntityNo"].ToString();
                    }
                    if (dt.Rows[n]["FEmplNo"] != null && dt.Rows[n]["FEmplNo"].ToString() != "")
                    {
                        model.FEmplNo = dt.Rows[n]["FEmplNo"].ToString();
                    }
                    if (dt.Rows[n]["FDeptNo"] != null && dt.Rows[n]["FDeptNo"].ToString() != "")
                    {
                        model.FDeptNo = dt.Rows[n]["FDeptNo"].ToString();
                    }
                    if (dt.Rows[n]["FAnswerDate"] != null && dt.Rows[n]["FAnswerDate"].ToString() != "")
                    {
                        model.FAnswerDate = dt.Rows[n]["FAnswerDate"].ToString();
                    }
                    if (dt.Rows[n]["FIp"] != null && dt.Rows[n]["FIp"].ToString() != "")
                    {
                        model.FIp = dt.Rows[n]["FIp"].ToString();
                    }
                    if (dt.Rows[n]["FSurverName"] != null && dt.Rows[n]["FSurverName"].ToString() != "")
                    {
                        model.FSurverName = dt.Rows[n]["FSurverName"].ToString();
                    }
                    if (dt.Rows[n]["FSurverContact"] != null && dt.Rows[n]["FSurverContact"].ToString() != "")
                    {
                        model.FSurverContact = dt.Rows[n]["FSurverContact"].ToString();
                    }
                    if (dt.Rows[n]["FSurverUnitName"] != null && dt.Rows[n]["FSurverUnitName"].ToString() != "")
                    {
                        model.FSurverUnitName = dt.Rows[n]["FSurverUnitName"].ToString();
                    }
                    if (dt.Rows[n]["FSurverAddress"] != null && dt.Rows[n]["FSurverAddress"].ToString() != "")
                    {
                        model.FSurverAddress = dt.Rows[n]["FSurverAddress"].ToString();
                    }
                    if (dt.Rows[n]["FSurverPostCode"] != null && dt.Rows[n]["FSurverPostCode"].ToString() != "")
                    {
                        model.FSurverPostCode = dt.Rows[n]["FSurverPostCode"].ToString();
                    }
                    if (dt.Rows[n]["FSurverEMail"] != null && dt.Rows[n]["FSurverEMail"].ToString() != "")
                    {
                        model.FSurverEMail = dt.Rows[n]["FSurverEMail"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }
        #endregion

		#region  Method

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(decimal FPaperAnswerManagerID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from t_PaperAnswerManager");
			strSql.Append(" where FPaperAnswerManagerID=@FPaperAnswerManagerID");
			SqlParameter[] parameters = {
					new SqlParameter("@FPaperAnswerManagerID", SqlDbType.Decimal)
};
			parameters[0].Value = FPaperAnswerManagerID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}
        /// <summary>
        /// 是否存在该记录(根据ip和员工编号)
        /// </summary>
        public bool ExistsByIpAndEmplNo(string ipAddress,string emplNo,decimal paperId,string entityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(*) from t_PaperAnswerManager");
            strSql.Append(" where FIp=@FIp AND FEmplNo=@FEmplNo AND FPaperId=@FPaperId AND FEntityNo=@FEntityNo");
            SqlParameter[] parameters = {
					new SqlParameter("@FPaperId", SqlDbType.Decimal,9),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,255),
					new SqlParameter("@FEmplNo", SqlDbType.VarChar,30),
					new SqlParameter("@FIp", SqlDbType.VarChar,15)
                                        };
            parameters[0].Value = paperId;
            parameters[1].Value = entityNo;
            parameters[2].Value = emplNo;
            parameters[3].Value = ipAddress;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 是否存在该记录(根据ip)
        /// </summary>
        public bool ExistsByIp(string ipAddress, decimal paperId, string entityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(*) from t_PaperAnswerManager");
            strSql.Append(" where FIp=@FIp AND FPaperId=@FPaperId AND FEntityNo=@FEntityNo");
            SqlParameter[] parameters = {
					new SqlParameter("@FPaperId", SqlDbType.Decimal,9),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,255),
					new SqlParameter("@FIp", SqlDbType.VarChar,15)
                                        };
            parameters[0].Value = paperId;
            parameters[1].Value = entityNo;
            parameters[2].Value = ipAddress;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public decimal Add(ElingOA6_DSJCJ.Model.PaperAnswerManager model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into t_PaperAnswerManager(");
			strSql.Append("FPaperId,FEntityNo,FEmplNo,FDeptNo,FAnswerDate,FIp,FSurverName,FSurverContact,FSurverUnitName,FSurverAddress,FSurverPostCode,FSurverEMail)");
			strSql.Append(" values (");
			strSql.Append("@FPaperId,@FEntityNo,@FEmplNo,@FDeptNo,@FAnswerDate,@FIp,@FSurverName,@FSurverContact,@FSurverUnitName,@FSurverAddress,@FSurverPostCode,@FSurverEMail)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@FPaperId", SqlDbType.Decimal,9),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,255),
					new SqlParameter("@FEmplNo", SqlDbType.VarChar,30),
					new SqlParameter("@FDeptNo", SqlDbType.VarChar,30),
					new SqlParameter("@FAnswerDate", SqlDbType.VarChar,30),
					new SqlParameter("@FIp", SqlDbType.VarChar,15),
					new SqlParameter("@FSurverName", SqlDbType.NVarChar,30),
					new SqlParameter("@FSurverContact", SqlDbType.NVarChar,30),
					new SqlParameter("@FSurverUnitName", SqlDbType.NVarChar,60),
					new SqlParameter("@FSurverAddress", SqlDbType.NVarChar,60),
					new SqlParameter("@FSurverPostCode", SqlDbType.NVarChar,30),
					new SqlParameter("@FSurverEMail", SqlDbType.NVarChar,30)};
			parameters[0].Value = model.FPaperId;
			parameters[1].Value = model.FEntityNo;
			parameters[2].Value = model.FEmplNo;
			parameters[3].Value = model.FDeptNo;
			parameters[4].Value = model.FAnswerDate;
			parameters[5].Value = model.FIp;
			parameters[6].Value = model.FSurverName;
			parameters[7].Value = model.FSurverContact;
			parameters[8].Value = model.FSurverUnitName;
			parameters[9].Value = model.FSurverAddress;
			parameters[10].Value = model.FSurverPostCode;
			parameters[11].Value = model.FSurverEMail;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToDecimal(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(ElingOA6_DSJCJ.Model.PaperAnswerManager model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update t_PaperAnswerManager set ");
			strSql.Append("FPaperId=@FPaperId,");
			strSql.Append("FEntityNo=@FEntityNo,");
			strSql.Append("FEmplNo=@FEmplNo,");
			strSql.Append("FDeptNo=@FDeptNo,");
			strSql.Append("FAnswerDate=@FAnswerDate,");
			strSql.Append("FIp=@FIp,");
			strSql.Append("FSurverName=@FSurverName,");
			strSql.Append("FSurverContact=@FSurverContact,");
			strSql.Append("FSurverUnitName=@FSurverUnitName,");
			strSql.Append("FSurverAddress=@FSurverAddress,");
			strSql.Append("FSurverPostCode=@FSurverPostCode,");
			strSql.Append("FSurverEMail=@FSurverEMail");
			strSql.Append(" where FPaperAnswerManagerID=@FPaperAnswerManagerID");
			SqlParameter[] parameters = {
					new SqlParameter("@FPaperId", SqlDbType.Decimal,9),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,255),
					new SqlParameter("@FEmplNo", SqlDbType.VarChar,30),
					new SqlParameter("@FDeptNo", SqlDbType.VarChar,30),
					new SqlParameter("@FAnswerDate", SqlDbType.VarChar,30),
					new SqlParameter("@FIp", SqlDbType.VarChar,15),
					new SqlParameter("@FSurverName", SqlDbType.NVarChar,30),
					new SqlParameter("@FSurverContact", SqlDbType.NVarChar,30),
					new SqlParameter("@FSurverUnitName", SqlDbType.NVarChar,60),
					new SqlParameter("@FSurverAddress", SqlDbType.NVarChar,60),
					new SqlParameter("@FSurverPostCode", SqlDbType.NVarChar,30),
					new SqlParameter("@FSurverEMail", SqlDbType.NVarChar,30),
					new SqlParameter("@FPaperAnswerManagerID", SqlDbType.Decimal,9)};
			parameters[0].Value = model.FPaperId;
			parameters[1].Value = model.FEntityNo;
			parameters[2].Value = model.FEmplNo;
			parameters[3].Value = model.FDeptNo;
			parameters[4].Value = model.FAnswerDate;
			parameters[5].Value = model.FIp;
			parameters[6].Value = model.FSurverName;
			parameters[7].Value = model.FSurverContact;
			parameters[8].Value = model.FSurverUnitName;
			parameters[9].Value = model.FSurverAddress;
			parameters[10].Value = model.FSurverPostCode;
			parameters[11].Value = model.FSurverEMail;
			parameters[12].Value = model.FPaperAnswerManagerID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(decimal FPaperAnswerManagerID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from t_PaperAnswerManager ");
			strSql.Append(" where FPaperAnswerManagerID=@FPaperAnswerManagerID");
			SqlParameter[] parameters = {
					new SqlParameter("@FPaperAnswerManagerID", SqlDbType.Decimal)
};
			parameters[0].Value = FPaperAnswerManagerID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string FPaperAnswerManagerIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from t_PaperAnswerManager ");
			strSql.Append(" where FPaperAnswerManagerID in ("+FPaperAnswerManagerIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ElingOA6_DSJCJ.Model.PaperAnswerManager GetModel(decimal FPaperAnswerManagerID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 FPaperAnswerManagerID,FPaperId,FEntityNo,FEmplNo,FDeptNo,FAnswerDate,FIp,FSurverName,FSurverContact,FSurverUnitName,FSurverAddress,FSurverPostCode,FSurverEMail from t_PaperAnswerManager ");
			strSql.Append(" where FPaperAnswerManagerID=@FPaperAnswerManagerID");
			SqlParameter[] parameters = {
					new SqlParameter("@FPaperAnswerManagerID", SqlDbType.Decimal)
};
			parameters[0].Value = FPaperAnswerManagerID;

			ElingOA6_DSJCJ.Model.PaperAnswerManager model=new ElingOA6_DSJCJ.Model.PaperAnswerManager();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["FPaperAnswerManagerID"]!=null && ds.Tables[0].Rows[0]["FPaperAnswerManagerID"].ToString()!="")
				{
					model.FPaperAnswerManagerID=decimal.Parse(ds.Tables[0].Rows[0]["FPaperAnswerManagerID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["FPaperId"]!=null && ds.Tables[0].Rows[0]["FPaperId"].ToString()!="")
				{
					model.FPaperId=decimal.Parse(ds.Tables[0].Rows[0]["FPaperId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["FEntityNo"]!=null && ds.Tables[0].Rows[0]["FEntityNo"].ToString()!="")
				{
					model.FEntityNo=ds.Tables[0].Rows[0]["FEntityNo"].ToString();
				}
				if(ds.Tables[0].Rows[0]["FEmplNo"]!=null && ds.Tables[0].Rows[0]["FEmplNo"].ToString()!="")
				{
					model.FEmplNo=ds.Tables[0].Rows[0]["FEmplNo"].ToString();
				}
				if(ds.Tables[0].Rows[0]["FDeptNo"]!=null && ds.Tables[0].Rows[0]["FDeptNo"].ToString()!="")
				{
					model.FDeptNo=ds.Tables[0].Rows[0]["FDeptNo"].ToString();
				}
				if(ds.Tables[0].Rows[0]["FAnswerDate"]!=null && ds.Tables[0].Rows[0]["FAnswerDate"].ToString()!="")
				{
					model.FAnswerDate=ds.Tables[0].Rows[0]["FAnswerDate"].ToString();
				}
				if(ds.Tables[0].Rows[0]["FIp"]!=null && ds.Tables[0].Rows[0]["FIp"].ToString()!="")
				{
					model.FIp=ds.Tables[0].Rows[0]["FIp"].ToString();
				}
				if(ds.Tables[0].Rows[0]["FSurverName"]!=null && ds.Tables[0].Rows[0]["FSurverName"].ToString()!="")
				{
					model.FSurverName=ds.Tables[0].Rows[0]["FSurverName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["FSurverContact"]!=null && ds.Tables[0].Rows[0]["FSurverContact"].ToString()!="")
				{
					model.FSurverContact=ds.Tables[0].Rows[0]["FSurverContact"].ToString();
				}
				if(ds.Tables[0].Rows[0]["FSurverUnitName"]!=null && ds.Tables[0].Rows[0]["FSurverUnitName"].ToString()!="")
				{
					model.FSurverUnitName=ds.Tables[0].Rows[0]["FSurverUnitName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["FSurverAddress"]!=null && ds.Tables[0].Rows[0]["FSurverAddress"].ToString()!="")
				{
					model.FSurverAddress=ds.Tables[0].Rows[0]["FSurverAddress"].ToString();
				}
				if(ds.Tables[0].Rows[0]["FSurverPostCode"]!=null && ds.Tables[0].Rows[0]["FSurverPostCode"].ToString()!="")
				{
					model.FSurverPostCode=ds.Tables[0].Rows[0]["FSurverPostCode"].ToString();
				}
				if(ds.Tables[0].Rows[0]["FSurverEMail"]!=null && ds.Tables[0].Rows[0]["FSurverEMail"].ToString()!="")
				{
					model.FSurverEMail=ds.Tables[0].Rows[0]["FSurverEMail"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select FPaperAnswerManagerID,FPaperId,FEntityNo,FEmplNo,FDeptNo,FAnswerDate,FIp,FSurverName,FSurverContact,FSurverUnitName,FSurverAddress,FSurverPostCode,FSurverEMail ");
			strSql.Append(" FROM t_PaperAnswerManager ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" FPaperAnswerManagerID,FPaperId,FEntityNo,FEmplNo,FDeptNo,FAnswerDate,FIp,FSurverName,FSurverContact,FSurverUnitName,FSurverAddress,FSurverPostCode,FSurverEMail ");
			strSql.Append(" FROM t_PaperAnswerManager ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM t_PaperAnswerManager ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.FPaperAnswerManagerID desc");
			}
			strSql.Append(")AS Row, T.*  from t_PaperAnswerManager T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "t_PaperAnswerManager";
			parameters[1].Value = "FPaperAnswerManagerID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

